#include "ManejadorDeArboles.h"

ManejadorDeArboles::ManejadorDeArboles() {
    bhi = 0;
    bho = 0;
}

void ManejadorDeArboles::emitir8bits(unsigned int caracter) {
    bitset<8*sizeof(char)> bitSet = caracter;
	for (int i = 7; i >= 0; i--) {
        bho->siguienteBitEsUno(bitSet[i]);
    }
}

BitHandlerInput* ManejadorDeArboles::getBitHandlerInput() {
	return bhi;
}

BitHandlerOutput* ManejadorDeArboles::getBitHandlerOutput() {
	return bho;
}

void ManejadorDeArboles::setBitHandlerInput(BitHandlerInput & bithi) {
    this->bhi=&bithi;
    for(int x=0;x<256;x++)
        arboles[x].setBitHandlerInput(bithi);
    longitudes.setBitHandlerInput(bithi);
}

void ManejadorDeArboles::setBitHandlerOutput(BitHandlerOutput &bitho) {
    this->bho=&bitho;
    for(int x=0;x<256;x++)
        arboles[x].setBitHandlerOutput(bitho);
    longitudes.setBitHandlerOutput(bitho);
}

void ManejadorDeArboles::emitirCaracter(unsigned char caracter) {
	emitir8bits(caracter);
}

void ManejadorDeArboles::emitirCaracter(unsigned char caracter, unsigned char contexto) {
    arboles[(int)contexto].codificarCaracter(caracter);
}

void ManejadorDeArboles::emitirLongitud(unsigned char longitud) {
	this->longitudes.codificarCaracter(longitud);
}

unsigned int ManejadorDeArboles::leer8bits()
{
    unsigned char caracterSalida=0x00;
    for (int i = 8; i > 0; i--) {
	if (bhi->siguienteBitEsUno()) {
		caracterSalida = caracterSalida | (0x01 << (i-1));
	}
    }
	return (unsigned int)caracterSalida;
}

unsigned int ManejadorDeArboles::obtenerCaracter() {
	return (unsigned int)leer8bits();
}

unsigned int ManejadorDeArboles::obtenerCaracter(unsigned char contexto){
    return arboles[(int)contexto].decodificarCaracter();;
}

unsigned int ManejadorDeArboles::obtenerLongitud()
{
	return longitudes.decodificarCaracter();
}

ManejadorDeArboles::~ManejadorDeArboles() {

}
